package com.test.book.registry;

import com.test.book.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@ResponseBody
@Slf4j
public class ExceptionAdvice {

//    当有多个异常通知时，匹配顺序为当前类及其⼦类向上依次匹配

    @ExceptionHandler(Exception.class)
    public Result handler(Exception e){
        log.error("发生异常, e:", e);
        return Result.fail("内部错误, 请联系管理员");
    }

    @ExceptionHandler(NullPointerException.class)
    public Result handler2(Exception e){
        log.error("发生异常, e:", e);
        return Result.fail("发生空指针异常, 请联系管理员");
    }

    @ExceptionHandler(IndexOutOfBoundsException.class)
    public Result handler3(Exception e){
        log.error("发生异常, e:", e);
        return Result.fail("数组越界异常, 请联系管理员");
    }
}
